package 多线程11_测试ReentrantLock;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
 * @ClassName: testReentrantLock01
 * @Author: AllenSun
 * @Date: 2022/11/25 上午6:17
 */
public class testReentrantLock01 {

    public static void main(String[] args) {

        ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
        Lock readLock = lock.readLock();
        Lock writeLock = lock.writeLock();

        ConcurrentHashMap map=new ConcurrentHashMap<>();
        map.put("1","1");
        map.get("1");

        new Thread(()->{
            readLock.lock();
            readLock.tryLock();
            readLock.unlock();

            writeLock.lock();
            writeLock.tryLock();
            writeLock.unlock();
            /*lock.lock();
            drawMoney();
            lock.unlock();*/
        },"线程1").start();
        /*new Thread(()->{
            lock.lock();
            drawMoney();
            lock.unlock();
        },"线程2").start();*/
    }


    private static void drawMoney() {
        System.out.println(Thread.currentThread().getName()+"正在取钱...");
        sleep(3000);
        System.out.println(Thread.currentThread().getName()+"取完了");
    }

    private static void sleep(long millis) {
        try {
            Thread.sleep(millis);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

}
